<template>
	<view class="addEditForm">
		<view class="data-form bgfff">
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">采购单</text>
				<!-- <view class="data-form-input">
					<cpdd-select v-model="dataForm.selectVal" :disabled="optionDisabled" :list="selectList"
						keyname="name" valname="val"></cpdd-select>
				</view> -->
				<view class="data-form-input" @click="openSelect">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.purchaseId"
						:placeholder="optionDisabled?'-':'请选择采购单'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">标题</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.title"
						:placeholder="optionDisabled?'-':'请输入标题'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">备注</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.remark"
						:placeholder="optionDisabled?'-':'请输入备注'" placeholder-class="plStyle" />
				</view>
			</view>
			
		</view>
		<view class="right-area-footer2 flex-ac bgfff" v-if="!optionDisabled">
			<view class="saveBtn cancleBtn" @tap="handleCancle">取消</view>
			<view class="saveBtn" @tap="handleSubmit">保存</view>
		</view>
		<cpdd-wp-list ref="cpddwplistRef" :data-list="goodsList"></cpdd-wp-list>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				did: '',
				dataForm: { //表单
					purchaseId: '',
					title: '',
					remark: '',
					params: {
						products: []
					}
				},
				goodsList: [],
				cpddDateRangeVal: '',
				cpddDateRangeVal2: '',
				selectList: [], //上一页带过来，如果上一页用不到，可以在这里调用赋值
				optionDisabled: false,
			}
		},
		onLoad(options) {
			console.log('options', options);
			this.did = options.id || ''
			this.optionDisabled = options.op == 'look' ? true : false
			let title = options.op == 'look' ? '详情' : options.op == 'add' ? '新增' : '编辑'
			uni.setNavigationBarTitle({
				title
			})
			if (options.op != 'add') {
				this.loadDetail();
			}
			uni.$on('selectCgd', (data) => {
				console.log(data);
				this.dataForm.purchaseId = data.id;
				this.loadCgdDetail();
				this.$forceUpdate()
			})
		},
		onUnload() {
			uni.$off('selectCgd')
		},
		methods: {
			loadDetail() {
				this.$http.get(`/material/warehouseIn/${this.did}`).then(res => {
					if (res.code == 200) {
						let result = res.data;
						this.dataForm.id = result.id;
						this.dataForm.purchaseId = result.purchaseId;
						this.dataForm.title = result.title;
						this.dataForm.remark = result.remark;
						this.dataForm.params = result.params;
						this.goodsList = result.params.products;
					}
				})
			},
			loadCgdDetail() {
				this.$http.get(`/material/purchaseOrder/${this.dataForm.purchaseId}`).then(res => {
					if (res.code == 200) {
						this.dataForm.purchaseId = res.data.id;
						this.goodsList = res.data.params.products;
					}
				})
			},
			cgdChange(e) {
				this.dataForm.purchaseId = e.userId;
				this.dataForm.nickName = e.nickName;
			},
			openSelect() {
				// this.$refs.cgdSelectRef.open();
				this.$u.to(`/pages/ylgl/cgd-warp/index?type=select`)
			},
			handleCancle() {
				this.$u.back()
			},
			handleSubmit() {
				console.log('提交');
				let products = this.$refs.cpddwplistRef.getData();
				if(this.dataForm.id) {
					this.dataForm.params.products = products.map(e => {
						return {
							id: e.id,
							popId: e.popId,
							name: e.name,
							quantity: e.quantity,
							unit: e.unit,
							produceTime: e.produceTime,
							expireTime: e.expireTime,
							fType: e.fType,
							fTypeSub: e.fTypeSub,
							whetherImport: e.whetherImport
						}
					});
				}else {
					this.dataForm.params.products = products.map(e => {
						return {
							popId: e.id,
							name: e.name,
							quantity: e.quantity,
							unit: e.unit,
							produceTime: e.produceTime,
							expireTime: e.expireTime,
							fType: e.fType,
							fTypeSub: e.fTypeSub,
							whetherImport: e.whetherImport
						}
					});
				}
				
				if(!this.dataForm.purchaseId) return this.$u.msg("请选择采购单");
				if(!this.dataForm.title) return this.$u.msg("请输入入库单标题");
				this.realSumbit();
			},
			realSumbit() {
				if (this.dataForm.id) {
					this.$http.put('/material/warehouseIn', this.dataForm).then(res => {
						if (res.code == 200) {
							uni.$emit('reFlashList')
							this.$u.msg('修改成功')
							this.$u.back(1, 1000)
						}
					})
				} else {
					this.$http.post('/material/warehouseIn', this.dataForm).then(res => {
						if (res.code == 200) {
							uni.$emit('reFlashList')
							this.$u.msg('已新增')
							this.$u.back(1, 1000)
						}
					})
				}
			},

		}
	}
</script>

<style>

</style>